﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    public static class LuaCheckTool
    {
        private static ApplicationPathBind appbind;

        private static void AutoInit()
        {
            string[] paths = new string[]
            {
                "./Tools/luacheck.exe"
            };
            appbind = new ApplicationPathBind("Nirvana.External.LuaCheck", "LuaCheck", paths);
        }

        public static bool IsExist()
        {
            return !string.IsNullOrEmpty(appbind.GetAppPath());
        }

        public static string GetAppPath()
        {
            return appbind.GetAppPath();
        }
        /// <summary>
        /// 在Preferences面板上显示
        /// </summary>
        public static void ShowGUI()
        {
            appbind.Show();
        }

        public static void StartApplication(string filename, string args, Action<string,string> onComplete)
        {
    
            string apppath = appbind.GetAppPath();
            string s = args + filename;
            RunApplication(apppath, s, (servertype,msg) => {

                if (msg.StartsWith("Checking ") && msg.EndsWith(" OK"))
                {
                    return;
                }
                if (msg.StartsWith("Checking ") && msg.EndsWith(" warning"))
                {
                    return;
                }
                if (msg.StartsWith("Checking ") && msg.EndsWith(" warnings"))
                {
                    return;
                }
                if (msg.StartsWith("Checking ") && msg.EndsWith(" error"))
                {
                    return;
                }
                if (msg.StartsWith("Checking ") && msg.EndsWith(" errors"))
                {
                    return;
                }
                if (msg.StartsWith("Total: ") && msg.EndsWith(" file"))
                {
                    return;
                }
                onComplete(servertype,msg);

            });

        }


        public static void RunApplication(string filename,string args,Action<string, string> onComplete)
        {
            using (Process process = Process.Start(new ProcessStartInfo(filename, args) 
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                StandardOutputEncoding = Encoding.UTF8,
                StandardErrorEncoding = Encoding.UTF8
            })){
                process.OutputDataReceived += (sender, e) => {

                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        string data = e.Data.Trim();
                        if (!string.IsNullOrEmpty(data))
                        {
                            if (onComplete != null) onComplete("warn",data);
                        }
                    }
                };
                process.ErrorDataReceived += (sender, e) => {

                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        string data = e.Data.Trim();
                        if (!string.IsNullOrEmpty(data))
                        {
                            if (onComplete != null) onComplete("error",data);
                        }
                    }
                };
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                process.Close();
  
            }
        }
    }
}

